<template>
  <div class="edit-form edit-dialog">
    <el-form :model="state.editForm" ref="formRef" label-width="100px">
      <el-descriptions
        :column="2"
        border
        :colon="true"
        label-class-name="custom-description-label-160"
      >
        <el-descriptions-item>
          <template #label>
            <span class="colorRed">*</span>
            评定项目
          </template>
          <el-form-item
            prop="name"
            :rules="$formRules.input"
          >
            <el-input
              type="text"
              v-model="state.editForm.name"
              placeholder="请输入"
              maxlength="50"
            ></el-input>
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <span class="colorRed">*</span>
            类型
          </template>
          <el-form-item
            prop="typeCode"
            :rules="$formRules.input"
          >
            <pub-dict-select
              style="width:100%"
              v-model="state.editForm.typeCode"
              dict-code="YLJGPDMB-SZ"
              placeholder="请选择"
            />
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            启用
          </template>
          <el-form-item>
            <el-switch
              v-model="state.editForm.isEnable"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <span class="colorRed">*</span>
            级别
          </template>
          <el-form-item
            prop="level"
            :rules="$formRules.inputNumber"
          >
            <el-input-number
            style="width:100%"
              type="text"
              :precision="0"
              :min="0"
              v-model.number="state.editForm.level"
              placeholder="请输入"
            ></el-input-number>
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <span class="colorRed">*</span>
            排序
          </template>
          <el-form-item
            prop="sort"
            :rules="$formRules.inputNumber"
          >
            <el-input-number
            style="width:100%"
            :precision="0"
              type="text"
              :min="0"
              v-model.number="state.editForm.sort"
              placeholder="请输入"
            ></el-input-number>
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <span class="colorRed">*</span>
            分数
          </template>
          <el-form-item
            prop="score"
            :rules="$formRules.inputNumber"
          >
            <el-input-number
            style="width:100%"
            :precision="0"
              type="text"
              :min="0"
              v-model.number="state.editForm.score"
              placeholder="请输入"
            ></el-input-number>
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template #label>
            评定方式说明
          </template>
          <el-form-item>
            <el-input
              type="textarea"
              maxlength="200"
              show-word-limit
              v-model="state.editForm.scoreTypeExplain"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template #label>
            备注
          </template>
          <el-form-item>
            <el-input
              type="textarea"
              maxlength="200"
              show-word-limit
              v-model="state.editForm.remark"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-descriptions-item>
        </el-descriptions>
    </el-form>
    <div class="btn-group">
      <el-button type="default" size="small" @click="emit('cancel')">
        取消
      </el-button>
      <el-button type="primary" size="small" @click="submitForm">
        保存
      </el-button>
      
    </div>
  </div>
</template>
<script setup>
import { reactive, ref } from '@vue/reactivity'
import { onMounted } from '@vue/runtime-core'
import { ElMessage } from 'element-plus'
import {
  yljgStarRatingDetail,
  yljgStarRatingEdit,
  yljgStarRatingAdd
} from '@/api/system'
const emit = defineEmits(['cancel'])
const props = defineProps({
  formData: {
    type: Object
  },
  formType: {
    type: String
  }
})
const state = reactive({
  editForm: {
    isEnable: 1
  }
})
const formRef = ref(null)
const initForm = async id => {
  let res = await yljgStarRatingDetail(id)
  state.editForm = res.data
}
const submitForm = () => {
  formRef.value.validate(async valid => {
    if (valid) {
      let res =
        props.formType == 'add'
          ? await yljgStarRatingAdd(state.editForm)
          : await yljgStarRatingEdit(state.editForm)
      if (res.succeed) {
        ElMessage.success(res.msg)
        emit('cancel')
        emit('success')
      }
    }
  })
}
onMounted(() => {
  //   state.editForm = props.formData
  if (props.formType == 'add') {
    state.editForm.parentId = props.formData.parentId
  } else {
    initForm(props.formData.id)
  }
})
</script>
<style lang="scss" scoped>
.btn-group {
  text-align: center;
}
</style>
